package com.stars.empoly.vmodel

import android.app.Application
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.componnent.CpAlipay
import com.stars.empoly.model.PlaceOrder
import com.stars.empoly.utils.WeChatUtil
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch

/**
 *
 *on 2020/9/22
 */
class PayVModel(application: Application) :  BViewModel<BRepository>(application) {


    var order_id: String? = null

    var isBalance: MutableLiveData<Boolean> = MutableLiveData(false)

    var isAlipay: MutableLiveData<Boolean> = MutableLiveData(true)

    var isWx: MutableLiveData<Boolean> = MutableLiveData(false)

    var choseType = 1//1支付宝 2微信


    var ll: LinearLayout? = null


    var totalValue: MutableLiveData<String> = MutableLiveData("")


    var total = ""

    var bean: PlaceOrder?=null









    fun toChose(type: Int) {//1支付宝 2微信 3余额支付
        choseType = type
        when (type) {
            1 -> {

                isBalance.value = false
                isAlipay.value = true
                isWx.value = false

            }
            2 -> {
                isBalance.value = false
                isAlipay.value = false
                isWx.value = true

            }
            3 -> {
                isBalance.value = true
                isAlipay.value = false
                isWx.value = false


            }


        }
    }



    fun toPay() {
        //去支付

        if(null!=bean){//先去下单再去支付

            toPlaceOrder()
        }else {
            getInfoPay("")
        }

    }



    fun  toPlaceOrder(){


        viewModelScope.launch {

            var it = repository().lreq {
                NetEngine.service.orderAdd(bean?.vehicle_model, bean?.vehicle_status,
                        bean?.start_city_name,  bean?.start_lat, bean?.start_lng,
                        bean?.end_city_name, bean?.end_lat, bean?.end_lng,
                        bean?.consignor_address, bean?.consignor_position,
                        bean?.end_address, bean?.end_position,
                        bean?.payment, bean?.remarks, bean?.start_driving_time,
                        bean?.end_driving_time, bean?.type_id,
                        bean?.consignor_name, bean?.consignor_mobile, bean?.update_amount,
                        bean?.receiving_name, bean?.receiving_mobile)
            }

            if (it.ok()) {

                order_id=it.data?.order_id
                getInfoPay("")
            }


        }


    }




    fun getInfoPay(password: String) {

        var type="wechat"//微信
        if(choseType==1){//支付宝
            type="alipay"
        }

        //todo


        viewModelScope.launch {
            var it = repository().req { NetEngine.service.orderPay(order_id, totalValue.value,type) }

            if (it.ok()) {

                if (choseType == 1) {//支付宝

                    val orderInfo = it.data!!.alipay
                    CpAlipay.pay(repository().context!!, orderInfo)

                } else if (choseType == 2) {//微信
                    if (WeChatUtil.isWeixinAvilible(repository().context)) {
                        WeChatUtil.toWXPay(repository().context, it.data!!)
                    }

                }


            }
        }
    }


}
